#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.shortcuts import render, HttpResponse
from teach.models import Table
import json
'''
modifi: $("#modifi").text(),
data: hot1.getData(),
merge
'''
class  Responser:
    def __init__(self):
        self.data = {}
        self.code = 200

    @property
    def dict(self):
        return self.__dict__


def distribute_list(request):
    """
    排课列表
    :param request:
    :return:
    """
    table_list = Table.objects.filter(table_type=1)
    if table_list:
        return render(request, 'teach/distribute_list.html',{"table_list":table_list})
    else:
        table_obj = Table.objects.create(table_type=1)
        table_obj.name = "工作区" + str(table_obj.id)
        table_obj.save()
        table_list = Table.objects.filter(table_type=1)
        return render(request, 'teach/distribute_list.html', {"table_list": table_list})



def set_table(request,id):
    if request.method =="POST":
        req_info = json.loads(str(request.body, encoding="utf8"))
        data = req_info.get("data")
        merge = req_info.get("merge")
        modifi = req_info.get("modifi")
        style = req_info.get("style")
        rep = Responser()
        table_obj =Table.objects.filter(id=id).first()
        if table_obj.modifi == modifi:
            table_obj.text = json.dumps(data)
            table_obj.merge = json.dumps(merge)
            table_obj.style = json.dumps(style)
            table_obj.save()

            rep.code = 200
            rep.data = "保存成功"
            rep.modifi = table_obj.modifi
        else:
            rep.code = 400
            rep.data = "保存失败, 其他用户已修改"
            rep.modifi = table_obj.modifi


        return HttpResponse(json.dumps(rep.dict))



def get_sheet(request,id):
    table_query = Table.objects.all()
    if table_query:
        table_obj = table_query.filter(id=id).first()
        dic = {"code":200,"data": table_obj.text,"merge":table_obj.merge,"modifi":table_obj.modifi,"style":table_obj.style}
        return HttpResponse(json.dumps(dic, ensure_ascii=True))
    else:
        dic = {"code": 400}
        return HttpResponse(json.dumps(dic, ensure_ascii=True))


def add_sheet(request):
    if request.method == "POST":
        table_obj = Table.objects.create()
        table_obj.name = "工作区"+str(table_obj.id)
        table_obj.save()
        dic = {"code":200,"id":table_obj.id,"name":table_obj.name,"modifi":table_obj.modifi}
        return HttpResponse(json.dumps(dic, ensure_ascii=True))

def del_table(request, id):
    if request.method == "POST":
        Table.objects.filter(id=id).delete()
        dic = {"code":200}
        return HttpResponse(json.dumps(dic, ensure_ascii=True))

def rename(request, id):
    if request.method == "POST":
        table_obj = Table.objects.get(id=id)
        table_obj.name = request.POST.get("name")
        table_obj.save()
        dic = {"code":200, "name":table_obj.name, "modifi":table_obj.modifi}
        return HttpResponse(json.dumps(dic, ensure_ascii=True))

class Base(object):
    pass


class F1(Base):
    pass


class F2(Base):
    pass


class F3(F2):
    pass


class F4(Base):
    pass


class F5(object):
    pass


class F6(F5):
    pass
